package com.zt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.mapper.TeacherAvailableMapper;
import com.zt.pojo.TeacherAvailable;
import com.zt.service.TeacherAvailableService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 教师可用时间服务实现类
 * @author system
 * @description 针对表【teacher_available(教师可用时间表)】的数据库操作Service实现
 */
@Service
public class TeacherAvailableServiceImpl extends ServiceImpl<TeacherAvailableMapper, TeacherAvailable>
        implements TeacherAvailableService {

    @Override
    public List<TeacherAvailable> getAvailableTimesByTeacherId(Integer teacherId) {
        LambdaQueryWrapper<TeacherAvailable> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeacherAvailable::getTeacherId, teacherId)
               .orderByAsc(TeacherAvailable::getTeacherAvailableTime);
        return this.list(wrapper);
    }

    @Override
    public boolean addAvailableTime(Integer teacherId, LocalDateTime availableTime, Integer status) {
        // 检查是否已存在相同的时间
        LambdaQueryWrapper<TeacherAvailable> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeacherAvailable::getTeacherId, teacherId)
               .eq(TeacherAvailable::getTeacherAvailableTime, availableTime);

        TeacherAvailable existing = this.getOne(wrapper);
        if (existing != null) {
            // 如果已存在，更新状态
            existing.setStatus(status);
            existing.setUpdateTime(new Date());
            return this.updateById(existing);
        } else {
            // 如果不存在，新增
            TeacherAvailable teacherAvailable = new TeacherAvailable();
            teacherAvailable.setTeacherId(teacherId);
            teacherAvailable.setTeacherAvailableTime(availableTime);
            teacherAvailable.setStatus(status);
            teacherAvailable.setCreateTime(new Date());
            teacherAvailable.setUpdateTime(new Date());
            return this.save(teacherAvailable);
        }
    }

    @Override
    public boolean removeAvailableTime(Integer teacherId, LocalDateTime availableTime) {
        LambdaQueryWrapper<TeacherAvailable> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeacherAvailable::getTeacherId, teacherId)
               .eq(TeacherAvailable::getTeacherAvailableTime, availableTime);
        return this.remove(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddAvailableTimes(Integer teacherId, List<LocalDateTime> availableTimes, Integer status) {
        if (availableTimes == null || availableTimes.isEmpty()) {
            return true;
        }

        // 先删除该教师的所有可用时间
        LambdaQueryWrapper<TeacherAvailable> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(TeacherAvailable::getTeacherId, teacherId);
        this.remove(deleteWrapper);

        // 批量插入新的可用时间
        List<TeacherAvailable> teacherAvailables = availableTimes.stream()
                .map(time -> {
                    TeacherAvailable teacherAvailable = new TeacherAvailable();
                    teacherAvailable.setTeacherId(teacherId);
                    teacherAvailable.setTeacherAvailableTime(time);
                    teacherAvailable.setStatus(status);
                    teacherAvailable.setCreateTime(new Date());
                    teacherAvailable.setUpdateTime(new Date());
                    return teacherAvailable;
                })
                .collect(Collectors.toList());

        return this.saveBatch(teacherAvailables);
    }

    @Override
    public boolean applyForLongTermTeacher(Integer teacherId) {
        // 检查是否已有申请记录
        LambdaQueryWrapper<TeacherAvailable> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeacherAvailable::getTeacherId, teacherId)
               .eq(TeacherAvailable::getStatus, 3); // 申请成为长期教师

        TeacherAvailable existing = this.getOne(wrapper);
        if (existing != null) {
            // 如果已有申请，更新申请时间
            existing.setApplyTime(new Date());
            existing.setUpdateTime(new Date());
            existing.setAuditStatus(0); // 重置为待审核
            return this.updateById(existing);
        } else {
            // 创建新的申请记录
            TeacherAvailable application = new TeacherAvailable();
            application.setTeacherId(teacherId);
            application.setStatus(3); // 申请成为长期教师
            application.setAuditStatus(0); // 待审核
            application.setApplyTime(new Date());
            application.setCreateTime(new Date());
            application.setUpdateTime(new Date());
            return this.save(application);
        }
    }



    @Override
    public TeacherAvailable getLongTermApplicationByTeacherId(Integer teacherId) {
        LambdaQueryWrapper<TeacherAvailable> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeacherAvailable::getTeacherId, teacherId)
               .eq(TeacherAvailable::getStatus, 3) // 申请成为长期教师
               .orderByDesc(TeacherAvailable::getApplyTime)
               .last("LIMIT 1");
        return this.getOne(wrapper);
    }
}
